package com.cnhis.cloudhealth.clinical.hosnurse.hoscare.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hoscare.dao.HosCareDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONObject;

@SuppressWarnings({"unchecked", "rawtypes"})
@Service
public class HosCareBo
{
    @Resource
    private HosCareDao hosCareDao;
    
    @Resource
    private GetNewId getNewId;
    
    public ModelVo getInTimeAndOutTime(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = hosCareDao.getInTimeAndOutTime(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(ls);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(ls);
        return mo;
    }
    
    public ModelVo queryPatientCareList(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = new Page();
        try
        {
            // String EDate = DateUtils.addInteger(map.get("SDate").toString(), Calendar.DATE, 2);
            // map.put("EDate",EDate);
            pg = hosCareDao.queryPatientCareList(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(pg.getList());
        mo.setTotal(pg.getTotalNumber());
        mo.setRecords(pg.getTotalNumber());
        return mo;
    }
    
    private void saveVCF1(Map map)
        throws Exception
    {
        hosCareDao.saveVCF1(map);
    }
    
    private void updateVCF1(Map map)
        throws Exception
    {
        hosCareDao.updateVCF1(map);
    }
    
    public ModelVo execEmrVcf1Insert(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        JSONObject json = JSONObject.fromObject(map.get("nText").toString());
        List<Map<String, Object>> vcfList = JsonUtil.getList(json.getJSONArray("VCF1").toString());
        int Insert0 = 0;
        for (Map<String, Object> m : vcfList)
        {
            if (m.get("vcf01") == null || "".equals(m.get("vcf01").toString()) || "0".equals(m.get("vcf01").toString()))
            {
                m.put("vcf01", getNewId.nextId());
                m.put("vcf10", DateUtils.getDateString(new Date()));
            }
            else
            {
               // m.put("vcf01", 0);
                Insert0++;
            }
        }
        map.put("vcfList", vcfList);
        //校验vcfList的时间是否重复、或者库里已经存在
        List<Map<String, Object>> jiaoyanList = hosCareDao.queryForList("HosCare.jiaoyanVcf1_1", map);
        String msg = "";
        if(jiaoyanList != null && !jiaoyanList.isEmpty())
        {
            for(Map<String, Object> temp : jiaoyanList)
            {
                if("01".equals(temp.get("vcf11")))
                {
                    msg += temp.get("vaa05") + "在" + temp.get("vcf09") + "已经录入过入院记录<br/>";
                }
                else if("06".equals(temp.get("vcf11")))
                {
                    msg += temp.get("vaa05") + "在" + temp.get("vcf09") + "已经录入过出院记录<br/>";
                }
            }
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg(msg);
            return mo;
        }
        
        if (Insert0 > 0)
        {
            // 更新护理记录
            updateVCF1(map);
        }
        else
        {
         // 新增
            saveVCF1(map);
        }
        return mo;
    }
    
    public ModelVo jiaoyanVcf1List(HashMap map) throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        JSONObject json = JSONObject.fromObject(map.get("nText").toString());
        List<Map<String, Object>> vcfList = JsonUtil.getList(json.getJSONArray("VCF1").toString());
        int Insert0 = 0;
        for (Map<String, Object> m : vcfList)
        {
            if (m.get("vcf01") == null || "".equals(m.get("vcf01").toString()) || "0".equals(m.get("vcf01").toString()))
            {
                m.put("vcf01", getNewId.nextId());
                m.put("vcf10", DateUtils.getDateString(new Date()));
            }
            else
            {
               // m.put("vcf01", 0);
                Insert0++;
            }
        }
        map.put("vcfList", vcfList);
        //校验vcfList的时间是否重复、或者库里已经存在
        List<Map<String, Object>> jiaoyanList = hosCareDao.queryForList("HosCare.jiaoyanVcf1", map);
        if(jiaoyanList != null && !jiaoyanList.isEmpty())
        {
            String msg = "";
            for(Map<String, Object> temp : jiaoyanList)
            {
                msg += temp.get("vaa05") + "在" + temp.get("vcf09") + "已经录入过护理记录<br/>";
            }
            mo.setResultMsg(msg);
        }
        return mo;
}
    
    public ModelVo queryTemperatureRecord(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = hosCareDao.queryTemperatureRecord(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo queryOPSRecord(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = hosCareDao.queryOPSRecord(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo patientCare(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = hosCareDao.patientCare(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(ls);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(ls);
        return mo;
    }
    
    private boolean existsVcf(Map map)
        throws Exception
    {
        return hosCareDao.existsVcf(map) > 0;
    }
    
    public ModelVo execEmrVcf1Delete(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        try
        {
            if (existsVcf(map))
            {
                mo.setResult(StaticKeys.OH_FALSE);
                mo.setResultMsg("要删除的护理记录已经归档，删除失败");
                return mo;
            }
            // --删除病人护理记录
            hosCareDao.deleteVCF1(map);
            // --删除病人护理文字记录
            hosCareDao.deleteVCG1(map);
            // --删除病人自定义护理项目
            hosCareDao.deleteVCK1(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            mo.setResultMsg("删除失败");
            return mo;
        }
        
        return mo;
    }
    
    public ModelVo execEmrVcf1ahUpdate(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        JSONObject VCF1 = JSONObject.fromObject(map.get("nText").toString());
        List VCF2 = JsonUtil.getList(VCF1.getJSONArray("VCF1").toString());
        map.put("VCF2", VCF2);
        hosCareDao.updateVCF2(map);
        
        return mo;
    }
    
    public ModelVo patientList(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = hosCareDao.patientList(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(ls);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(ls);
        mo.setTotal(ls.size());
        return mo;
    }
    
    public ModelVo execEmrvcf1update(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
//        List<Map<String, Object>> VCF1 = JsonUtil.getList(map.get("nText").toString());
        JSONObject json = JSONObject.fromObject(map.get("nText").toString());
        List<Map<String, Object>> VCF1 = JsonUtil.getList(json.getJSONArray("VCF1").toString());
//        List<Map<String, Object>> VCK1 = JsonUtil.getList(VCF2.getJSONArray("VCK1").toString());
//        for (Map<String, Object> m : VCF1)
//        {
//            if (m.containsKey("vcf09"))
//            {
//                m.put("vcf09", m.get("vcf09").toString() + " " + m.get("time").toString()+":00:00");
//            }
//        }
        map.put("VCF1", VCF1);
        long CBF01 = 0l;
        if (null != map.get("CBF01") && !"".equals(map.get("CBF01").toString()))
        {
            CBF01 = Long.parseLong(map.get("CBF01").toString());
        }
        List<Map<String, Object>> tmpVAQ1 = getVAQ1(map);
        int count = getCount(tmpVAQ1);
        if (count > 0)
        {
            tmpVAQ1 = getTmpVAQ1(CBF01, tmpVAQ1, map);
        }
       
        map.put("tmpVAQ1", tmpVAQ1);
        VCF1 = getVCF1(map);
        map.put("VCF1", VCF1);
//        VCK1 = getVCK1(map);
//        map.put("tmpVCK1", VCK1);
        // 新增病历记录
        if (!"0".equals(map.get("CAD01").toString()))
        {
            hosCareDao.insertVAQ1(map);
        }
        // 更新护理记录
        hosCareDao.update_VCF1(map);
        // 新增病历附表
//        hosCareDao.inset_VCK1(map);
        return mo;
        
    }
    
    private List getVCK1(Map map)
        throws Exception
    {
        List<Map<String, Object>> VCK1 = hosCareDao.getVCK1(map);
        for (Map<String, Object> m : VCK1)
        {
            if (m.get("vck01") == null || "0".equals(m.get("vck01").toString()))
            {
                Long newID = getNewId.nextId();
                m.put("vck01", newID);
                m.put("accessmode", 0);
                m.put("id", newID + 1);
            }
        }
        return VCK1;
    }
    
    private List getVCF1(Map map)
        throws Exception
    {
        List<Map<String, Object>> tmpVCF1 = hosCareDao.getVCF1(map);
        for (Map<String, Object> m : tmpVCF1)
        {
            if (m.containsKey("accessmode"))
            {
                if (m.get("accessmode").toString().equals("0"))
                {
                    Long newID = getNewId.nextId();
                    m.put("vcf01", newID);
                    m.put("id", newID + 1);
                }
            }
        }
        return tmpVCF1;
    }
    
    private List getTmpVAQ1(long CBF01, List<Map<String, Object>> tmpVAQ1, Map map)
        throws Exception
    {
        String CAD04 = null;
        if (CBF01 > 0)
        {
            CAD04 = hosCareDao.getCAD041(map);
        }
        else
        {
            CAD04 = hosCareDao.getCAD042(map);
        }
        for (Map<String, Object> m : tmpVAQ1)
        {
            if (m.containsKey("vaq01"))
            {
                if ("0".equals(m.get("vaq01").toString()))
                {
                    Long id = getNewId.nextId();
                    m.put("accessmode", 0);
                    m.put("vaq01", id);
                    m.put("acf01", 2);
                    m.put("cad04", CAD04);
                    m.put("id", id + 1);
                }
            }
        }
        return tmpVAQ1;
    }
    
    private List getVAQ1(Map map)
        throws Exception
    {
        return hosCareDao.getVAQ1(map);
    }
    
    private int getCount(List<Map<String, Object>> VAQ1)
        throws Exception
    {
        int count = 0;
        for (Map<String, Object> m : VAQ1)
        {
            if ("0".equals(m.get("vaq01").toString()))
            {
                count++;
            }
        }
        
        return count;
    }
    
    private boolean checkSign()
        throws Exception
    {
        return hosCareDao.checkSign() > 0;
    }
    
    public ModelVo execEmrVcf1UpdateState(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        Map m = JsonUtil.parserToMap(JSONObject.fromObject(map.get("nText").toString()));
        map.putAll(m);
        String State = map.get("State").toString();
        if ("0".equals(State))
        {// 签名
            if (checkSign())
            {
                mo.setResult(StaticKeys.OH_FALSE);
                mo.setResultMsg("病人护理记录已经归档,签名失败");
                return mo;
            }
            // 签名
            hosCareDao.update_CBP1(map);
            
        }
        else if ("1".equals(State))
        {// 归档
            hosCareDao.updateCBP1(map);
        }
        else
        {// 取消归档
            hosCareDao.CBP1_update(map);
        }
        
        return mo;
        
    }

    public ModelVo queryCareItems(Map map)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = hosCareDao.queryCareItems(map);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo getHour(HashMap map)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = hosCareDao.getHour(map);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo updateVaj46(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            hosCareDao.update("HosCare.updateVaj46", map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    public ModelVo updateVaj21(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            hosCareDao.update("HosCare.updateVaj21", map);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryVapListByVaa07(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosCareDao.queryForList("HosCare.queryVapListByVaa07", map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryFenmian(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosCareDao.queryForList("HosCare.queryFenmian", map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryYingerEmr(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = hosCareDao.queryForList("HosCare.queryYingerEmr", map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryYingerCare(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            String vaa01 = map.get("vaa01").toString();
            List list = new ArrayList<>();
            if(vaa01!=null && Long.parseLong(vaa01)>0)
            {
                list = hosCareDao.queryForList("HosCare.queryYingerCare1", map);
            }
            else
            {
                list = hosCareDao.queryForList("HosCare.queryYingerCare2", map);
            }
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryChankeBingren(HashMap map)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List list = new ArrayList();
            list = hosCareDao.queryForList("HosCare.queryChankeBingren", map);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo updateVdi1(Map param)
    {
        ModelVo mo = new ModelVo();
        try
        {
        	List<Map> list = (List) param.get("vdiList");
        	List<Long> vdi01List = new ArrayList<>();
        	if(list!=null &&!list.isEmpty())
        	{
        		vdi01List = getNewId.nextId(list.size());
        	}
        	for(int i = 0;i<list.size();i++)
        	{
        		list.get(i).put("vdi01", vdi01List.get(i));
        	}
            hosCareDao.update("HosCare.updateVdi1", param);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo queryChancheng(HashMap param)
    {
        ModelVo mo = new ModelVo();
        try
        {
            List<Map> list = hosCareDao.queryForList("HosCare.queryChancheng", param);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResultMsg(e.toString());
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public void updateVcf18(Map<String, Object> map) throws Exception
    {
        if("3".equals(GlobalVariable.StringIsNull(map.get("type"))))
        {
            hosCareDao.update("HosCare.updateVcf18_3", map);
        }
        else if("1".equals(GlobalVariable.StringIsNull(map.get("type"))))
        {
            hosCareDao.update("HosCare.updateVcf18_1", map);
        }
        else if("2".equals(GlobalVariable.StringIsNull(map.get("type"))))
        {
            hosCareDao.update("HosCare.updateVcf18_2", map);
        }
    }

}
